package yhp.servlet;

import yhp.service.UserService;
import yhp.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = "/praise")
public class PraiseServlets extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接受参数

        String userName = req.getParameter("userName");
        String userPassword = req.getParameter("userPassword");
        //2.调取service方法
        UserService userService = new UserServiceImpl();
        //进行用户名查找，若用户名存在且点赞状态为1，再次点赞则状态变为0，若用户名不存在，则创建用户，点击点赞则点赞状态变为1
        if(userService.findByUserName(userName)){
            if(userService.getPraiseStatus(userName)==1){
                userService.changePraiseStatus(userName,0);
            }else{
                userService.changePraiseStatus(userName,1);
            }
        }else{
            userService.insert(userName,userPassword);
            userService.changePraiseStatus(userName,1);
        }
        //3.跳转页面
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(userService.sum());
    }
}
